/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski   *
 *   g.latarowski@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/



#ifndef WINDOW_H
#define WINDOW_H

// libc
#ifdef Q_OS_UNIX
#include <sys/time.h>   // hi-res clock (getitmeofday)
#endif

// Qt
#include <QtGui>
#include <QtXml>

// lib
#include "analyzer.h"
#include "autoclient.h"

// local
#include "ui_window.h"
#include "roieditor.h"


// -------------------------- Window{} ---------------------------------------
/// \brief Main window class.
// ---------------------------------------------------------------------------
class Window : public QMainWindow, protected Ui::Window
{
	Q_OBJECT
			
public:
	
	// ====================================================
	// constructor/destructor

	/// \brief Setups window.
	///
	/// Creates scene and adds it to view.
	Window();

	/// \brief Deletes window.
	virtual ~Window();


public slots:
	void updateFrame();
	void clientDisconnected();
	
protected slots:


	// ====================================================
	// actions

		// ------------------------------------------------
		// file manipulation

	/// \brief Creates new/empty configuration file.
	virtual void on_actionNew_triggered();
	/// \brief Opens configuration file.
	virtual void on_actionOpen_triggered();
	/// \brief Saves configuration file as.
	virtual void on_actionSaveAs_triggered();
	/// \brief Saves configuration file to _fileName.
	virtual void on_actionSave_triggered();
	/// \brief Loads an image.
	virtual void on_actionLoadImage_triggered();

		// ------------------------------------------------
		// connection
	virtual void on_toolConnect_clicked();

		// ------------------------------------------------
		// image manipulating actions
	
	/// \brief Fits view scale to show hole images width.
	virtual void on_actionZoomFitToWidth_triggered();
	/// \brief Fits view scale to show hole images height.
	virtual void on_actionZoomFitToHeight_triggered();
	/// \brief Zooms view in.
	virtual void on_actionZoomIn_triggered();
	/// \brief Zooms view out.
	virtual void on_actionZoomOut_triggered();
	/// \brief Sets scale to original, 1:1.
	virtual void on_actionZoomOriginalSize_triggered();

		// ------------------------------------------------
		// video
	virtual void on_actionPlay_triggered();
	virtual void on_actionStop_triggered();

		// ------------------------------------------------
		// settings 
	virtual void on_toolUpdateParams_clicked();

	
private:
	void zoomFitInWidget();

	// ====================================================
    // hi-res clock
    void startClock();                  ///< starts hi-res clock
    void endClock( const QString description );///< Ends clock, display result with decritpion
#ifdef Q_OS_UNIX
    struct timeval _clock;
#else
    DWORD _clock;
#endif
	
	// ====================================================
	// xml config files

	QString _configFileName;
	
	bool readConfigFile ( QIODevice * pDevice );
	bool writeConfigFile( QIODevice * pDevice );
	
	bool createCameraElements( QDomElement & camera );
	bool createCameraIdentyfication( QDomElement & settings );
	bool createCameraSettings( QDomElement & settings );
	bool createCameraRoi( QDomElement & roi );
	bool createCameraMarkers( QDomElement & markers );
	bool createPoints( QDomElement & parent, const QPolygonF & points );

	bool parseCameraElements( QDomElement & camera );
	bool parseCameraIdentyfication( QDomElement & identyfication );
	bool parseCameraSettings( QDomElement & settings );
	bool parseCameraRoi( QDomElement & roi );
	bool parseCameraMarkers( QDomElement & markers );
	bool parsePoints( QDomElement & parent, QPolygonF * pPoints );

	QDomDocument * _pDocConfig; ///< XML document file


	// ====================================================
	// scene roi editor
	
	GRoiEditor * _pRoiEditor; ///< ROI editor

	// ====================================================
	// camera related
	GAutoClient _autoClient;
	QTimer      _updateTimer;
	
	QVariantMap capsulateParams();
	
}; // class !Window
// ---------------------------------------------------------------------------




#endif // !WINDOW_H






// eof
